using System;
using System.ComponentModel;
using System.Globalization;

namespace Rhizome.Model
{
	public sealed class MessageTypeConverter : TypeConverter
	{
		#region Overrides of TypeConverter

		public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
		{
			return sourceType == typeof (string);
		}

		public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
		{
			if (value == null) throw new ArgumentNullException("value");
			
			var s = ((string) value).Trim();
			var args = s.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);
			var id = args[0];

			var m = new Message(id);
			if (args.Length == 2) m.Data = args[1];
			return m;
		}

		#endregion
	}
}